import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*Name:   Baljeet Singh
 *No:     562632
 *Class:  OOSD
 *Group:  BTM Technologies
 *Title:  PROJ207 Threaded Workshop Project
 *Date :  March,9,2011   
 */

public class Database_Functions
{
	private static Connection conn;
	private static Statement stmt;
	private static ResultSet rs;
	//Function that connects with the oracle database
	public static Statement databaseConnection()
	{
		try 
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "ictoosd", "ictoosd");
			stmt = conn.createStatement();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stmt;
	}
	
	public static ResultSet selectQuery(String table,String order,String antiorder,String condition)
	{
		String sql="";
		if(table=="packages")
		{
			sql = "Select * from "+table+" order by "+order;
		}
		else
		{
			sql = "Select * from "+table+" where 1=1 ";
			if(!condition.equals(""))
			{
				sql+="and not "+order+" in(select "+order+" from products_suppliers where "+antiorder+" = "+condition+")";
			}
			sql+=" order by "+order;
			
		}
		
		try 
		{
			rs = stmt.executeQuery(sql);
			
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	public static ResultSet selectprodsupp(String sql)
	{
		try 
		{
			rs = stmt.executeQuery(sql);
			
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
}
